package com.mlnx.netty.maibb.uitl;


import com.mlnx.netty.maibb.handler.MaibbServerHandle;
import com.mlnx.netty.maibb.pojo.MemoryData;
import com.mlnx.netty.maibb.pojo.SingleData;

import java.nio.ByteBuffer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class KangtaiBpPacket {



    private ByteBuffer packetBuff;
    private static SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmm");
    public KangtaiBpPacket() {
        packetBuff = ByteBuffer.allocate(500);
        packetBuff.flip();
    }

    public static SingleData checkCode(byte[] byteMessage) throws ParseException {
        if(byteMessage.length<7)
            return null;
        if (byteMessage[7] == 1) {
            //新版协议设备发送的是身份数据帧
            //普通身份应答
            byte[] bytes = new byte[7];
            bytes[0] = (byte) 170;
            bytes[1] = (byte) 128;
            bytes[2] = 0x05;
            bytes[3] = 0x02;
            bytes[4] = 0x01;
            bytes[5] = 0x00;
            bytes[6] = 0x06;
            SingleData singleData = new SingleData();
            singleData.setCode(bytes);
            System.out.println("已成功连接...");
            return singleData;

        }

        if (byteMessage[7] == 2) {
            //时间校准身份应答
            String time = CodeUtil.getDeviceTime4New(byteMessage);
            String startTime = DateUtil.getToday(BusiConstants.System_Formate.Formate_Date_yyMMddHHmm);
            long result = EasyStr.differenceLong(time, startTime);
            byte[] ordinaryTime = new byte[12];
            Calendar cal = Calendar.getInstance();
            String sy = DateUtil.getToday("yy");
            ordinaryTime[0] = (byte) 170;
            ordinaryTime[1] = (byte) 128;
            ordinaryTime[2] = 0x05;
            ordinaryTime[3] = 7;
            ordinaryTime[4] = 0x01;
            ordinaryTime[5] = 0x02;
            ordinaryTime[6] = Byte.parseByte(sy);
            ordinaryTime[7] = Byte.parseByte(String.valueOf(cal.get(Calendar.MONTH) + 1));// 获取月份
            ordinaryTime[8] = Byte.parseByte(String.valueOf(cal.get(Calendar.DATE)));// 获取日
            ordinaryTime[9] = Byte.parseByte(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)));// 小时
            ordinaryTime[10] = Byte.parseByte(String.valueOf(cal.get(Calendar.MINUTE)));// 分
            ordinaryTime[11] = (byte) (ordinaryTime[2] ^ ordinaryTime[3] ^ ordinaryTime[4] ^ ordinaryTime[5] ^ ordinaryTime[6] ^ ordinaryTime[7] ^ ordinaryTime[8] ^ ordinaryTime[9] ^ ordinaryTime[10]);

            SingleData singleData = new SingleData();
            singleData.setCode(ordinaryTime);
            return singleData;
        }

        if (byteMessage[7] == 3) {
            //设备发送的是单次血压计数据，并且是用的新版协议
            // 成功解析完血压数据，应答设备
            byte[] bytes = new byte[7];
            bytes[0] = (byte) 170;
            bytes[1] = (byte) 128;
            bytes[2] = 0x05;
            bytes[3] = 0x02;
            bytes[4] = 0x03;
            bytes[5] = 0x00;
            bytes[6] = 0x10;
            //设备编码
            String deviceNo = CodeUtil.getResultDevCode4New(byteMessage);
            int shou = (int) ((byteMessage[30] * 256) + byteMessage[31] & 0xff);
            int shu = (int) ((byteMessage[32] * 256) + byteMessage[33] & 0xff);
            int pulse = (int) ((byteMessage[34] & 0xff));
            Date devTime = EasyStr.nowDate();
            String devDate = EasyStr.today();
            int nian = byteMessage[25] & 0xff;
            int yue = byteMessage[26] & 0xff;
            int r = byteMessage[27] & 0xff;
            int shi = byteMessage[28] & 0xff;
            int fen = byteMessage[29] & 0xff;
            String stringYear = IKnetUtil.cover(nian, 2);
            String stringMonth = IKnetUtil.cover(yue, 2);
            String stringR = IKnetUtil.cover(r, 2);
            String stringShi = IKnetUtil.cover(shi, 2);
            String stringfen = IKnetUtil.cover(fen, 2);
            String devtime = stringYear + stringMonth + stringR + stringShi + stringfen;
            System.out.println("设备编码+" + deviceNo + "收缩压:" + shou + "舒张压:" + shu + "心率:" + pulse + "测量时间" + devtime);
            SingleData singleData = new SingleData();
            singleData.setDeviceId(deviceNo);
            singleData.setCode(bytes);
            singleData.setSbp(shou);
            singleData.setDbp(shu);
            singleData.setPulse(pulse);
            singleData.setDate(format.parse(devtime));

            return singleData;
        }

        if (byteMessage[7] == 4) {
            //设备发送的是记忆中的数据,并且是用的新版协议
            //按照协议取记忆数据，demo只解析到只有一条记忆数据，记忆数组最多有十组，建议循坏取出。
            byte[] bytes = new byte[7];
            bytes[0] = (byte) 170;
            bytes[1] = (byte) 128;
            bytes[2] = 5;
            bytes[3] = 2;
            bytes[4] = 4;
            bytes[5] = 0;
            bytes[6] = 11;
            int len = byteMessage[25];
            List<MemoryData> memoryDatas = new ArrayList<>();
            //设备编码
            String deviceNo = CodeUtil.getResultDevCode4New(byteMessage);
            for(int i = 0; i < len;i++){
                int nian = byteMessage[25 + i*10 + 1] & 0xff;
                int yue = byteMessage[25 + i*10 + 2] & 0xff;
                int r = byteMessage[25 + i*10 + 3] & 0xff;
                int shi = byteMessage[25 + i*10 + 4] & 0xff;
                int fen = byteMessage[25 + i*10 + 5] & 0xff;
                int shou = (int) ((byteMessage[25 + i*10 + 6] * 256) + byteMessage[25 + i*10 + 7] & 0xff);
                int shu = (int) ((byteMessage[25 + i*10 + 8] * 256) + byteMessage[25 + i*10 + 9] & 0xff);
                int pulse = (int) ((byteMessage[25 + i*10 + 10] & 0xff));
                String stringYear = IKnetUtil.cover(nian, 2);
                String stringMonth = IKnetUtil.cover(yue, 2);
                String stringR = IKnetUtil.cover(r, 2);
                String stringShi = IKnetUtil.cover(shi, 2);
                String stringfen = IKnetUtil.cover(fen, 2);
                String devtime = stringYear + stringMonth + stringR + stringShi + stringfen;
                System.out.println("收缩压:" + shou + "舒张压:" + shu + "心率:" + pulse + "测量时间" + devtime);
                MemoryData memoryData = new MemoryData();
                memoryData.setSbp(shou);
                memoryData.setDbp(shu);
                memoryData.setPulse(pulse);
                memoryData.setDate(format.parse(devtime));
                memoryDatas.add(memoryData);
                memoryData.setDeviceId(deviceNo);
            }
            SingleData singleData = new SingleData();
            singleData.setMemoryData(memoryDatas);
            singleData.setCode(bytes);
            return singleData;
        }
        return null;
    }
}
